package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.StringUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CodeCountHelper;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.Utils.VerifityHelper;

/**
 * 修改联系号码界面
 */
public class ModifyPhoneView extends LFView implements View.OnClickListener {

    private Context mContext;

    private View mView;

    private TextView getCodeTV;

    private EditText newPhoneNumET;

    private EditText codeET;

    private Button bindBtn;

    private BaseApplication application;

    private String oldPhone;

    private VerifityHelper codeHelper;

    private CodeCountHelper helper;

    public ModifyPhoneView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_modifyphone, null);
        if (null == mView) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText(R.string.modify_phone);
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        TextView oldPHoneNumTV = (TextView) mView.findViewById(R.id.mphone_tv_curphone);
        getCodeTV = (TextView) mView.findViewById(R.id.mphone_tv_getcode);
        newPhoneNumET = (EditText) mView.findViewById(R.id.mphone_et_newphone);
        codeET = (EditText) mView.findViewById(R.id.mphone_et_code);
        bindBtn = (Button) mView.findViewById(R.id.mphone_btn_modify);

        setListener(true);

        if (null == codeHelper) {
            codeHelper = new VerifityHelper(mView, mContext);
            codeHelper.setTextSize(14f);
        }
        application = (BaseApplication) mContext.getApplicationContext();
        oldPhone = application.getLoginInfo().getBean("userinfo").getString("mobilephone");
        oldPHoneNumTV.setText(oldPhone);
    }

    public void setListener(boolean flag) {
        if (null != getCodeTV) {
            getCodeTV.setOnClickListener(flag ? this : null);
        }
        if (null != bindBtn) {
            bindBtn.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mphone_tv_getcode://获取验证码
                getCode();
                break;
            case R.id.mphone_btn_modify://修改手机号
                modify();
                break;
            default:
                break;
        }
    }

    /**
     * 获取修改手机验证码
     */
    public void getCode() {
        String phone = newPhoneNumET.getText().toString().trim();
        if (!StringUtils.isPhone(phone)) {
            Toast.makeText(mContext, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!codeHelper.checkPicCode()) {
            return;
        }
        String imgCode = codeHelper.getImgCode();
        String imageId = codeHelper.getPicBean().getString("imgid");
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(application).getPhoneCode(phone, imgCode, imageId,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean bean = (EntityBean) result;
                        String redes = bean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            Toast.makeText(mContext, redes, Toast.LENGTH_SHORT).show();
                        }
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            if (null == mView) {
                                return;
                            }
                            if (null == helper) {
                                helper = new CodeCountHelper(getCodeTV);
                            }
                            helper.startCount();
                        }
                    }
                });
    }

    /**
     * 修改联系手机号
     */
    public void modify() {
        final String phone = newPhoneNumET.getText().toString().trim();
        if (!StringUtils.isPhone(phone)) {
            Toast.makeText(mContext, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!codeHelper.checkPicCode()) {
            return;
        }
        String code = codeET.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(mContext, "请输入正确的短信验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        String token = application.getToken();
        String userflag = application.getKckpName();
        String pwd = application.getKckpPass();
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(application).modifyPhone(userflag, oldPhone, phone, code,
                token, pwd, new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(getContext(), error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean bean = (EntityBean) result;
                        String redes = bean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            Toast.makeText(mContext, redes, Toast.LENGTH_SHORT).show();
                        }
                        String restate = bean.getString("restate");
                        if (TextUtils.equals("1", restate)) {//修改成功
                            //重新设置手机号的值
                            application.getLoginInfo().getBean("userinfo").set("mobilephone", phone);
                            LSMsgCall(98, "modifyphone");
                            OnDestroy();
                            closeForm();
                        }
                    }
                });
    }

    @Override
    public void refresh() {
        codeHelper.loadPicCode();
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        setListener(false);
        if (null != helper) {
            helper.destroy();
            helper = null;
        }
        codeHelper = null;
    }
}
